//
//  ZZCNameController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCNameController.h"
#import "ZZCPropertyController.h"
#import "ZZCMeInfo.h"
@interface ZZCNameController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;

@end

@implementation ZZCNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"更改昵称";
    self.view.backgroundColor = ZZCBGColor;
    self.nameTextField.text = self.nameStr;
    [self.nameTextField becomeFirstResponder];
}

- (IBAction)submitClick {
    ZZCLog(@"你提交了名字");
    [self uploadRequest];
}
- (void)uploadRequest {
    if (self.nameTextField.text == nil) {
        [SVProgressHUD showInfoWithStatus:@"请输入更改的昵称"];
        return;
    }
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token,@"nickname":self.nameTextField.text};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"token":token,@"nickname":self.nameTextField.text};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_NICKNAME parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-**%@",responseObject);
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            if ([responseObject[@"code"] isEqual:@(200)]) {
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                [user setObject:self.nameTextField.text forKey:@"NICK_NAME"];

                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeNameSucceed" object:nil];
                [self.navigationController popViewControllerAnimated:YES];

            }
        }
    }];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
